#include "desktopitem.h"
#include <QPainter>
#include <QDebug>




DesktopItem::DesktopItem(int maxWidth, int maxHeight)
{
    this->maxHeight=maxHeight;
    this->maxWidth=maxWidth;


}

\

void DesktopItem::initDesktop()
{
    //决定棋盘大小
    double len=static_cast<double>(maxHeight>maxWidth?maxWidth:maxHeight);
    r=len/24;
    d=2*r;
    sx=1.5*d;
    sy=d;
    qDebug()<<"mw="<<maxWidth<<"mh="<<maxHeight<<"len="<<len<<"r="<<r<<"d="<<d<<"sx="<<sx<<"sy="<<sy;
}

void DesktopItem::setPossiblePos(QList<int *> &poses)
{
    clearPossiblePos();
    foreach (auto item, poses) {
        this->poses.append(new int[]{item[0],item[1]});
    }
}

void DesktopItem::clearPossiblePos()
{
    qDeleteAll(this->poses);
    this->poses.clear();
}




DesktopItem::~DesktopItem()
{
    qDeleteAll(this->poses);
    this->poses.clear();
}

//绘制折线
void drawLabelLine(double sx,double sy,int type,QPainter *painter,double d){

    if(type==0){
        //左边
        QPainterPath path1;
        path1.moveTo(sx+0.1*d,sy-0.3*d);
        path1.lineTo(sx+0.1*d,sy-0.1*d);
        path1.lineTo(sx+0.3*d,sy-0.1*d);
        painter->drawPath(path1);
        QPainterPath path2;
        path2.moveTo(sx+0.1*d,sy+0.3*d);
        path2.lineTo(sx+0.1*d,sy+0.1*d);
        path2.lineTo(sx+0.3*d,sy+0.1*d);
        painter->drawPath(path2);
    }else if(type==1){
        //中间
        QPainterPath path1;
        path1.moveTo(sx+0.1*d,sy-0.3*d);
        path1.lineTo(sx+0.1*d,sy-0.1*d);
        path1.lineTo(sx+0.3*d,sy-0.1*d);
        painter->drawPath(path1);
        QPainterPath path2;
        path2.moveTo(sx+0.1*d,sy+0.3*d);
        path2.lineTo(sx+0.1*d,sy+0.1*d);
        path2.lineTo(sx+0.3*d,sy+0.1*d);
        painter->drawPath(path2);
        QPainterPath path3;
        path3.moveTo(sx-0.1*d,sy-0.3*d);
        path3.lineTo(sx-0.1*d,sy-0.1*d);
        path3.lineTo(sx-0.3*d,sy-0.1*d);
        painter->drawPath(path3);
        QPainterPath path4;
        path4.moveTo(sx-0.1*d,sy+0.3*d);
        path4.lineTo(sx-0.1*d,sy+0.1*d);
        path4.lineTo(sx-0.3*d,sy+0.1*d);
        painter->drawPath(path4);
    }else{
        //右边
        QPainterPath path3;
        path3.moveTo(sx-0.1*d,sy-0.3*d);
        path3.lineTo(sx-0.1*d,sy-0.1*d);
        path3.lineTo(sx-0.3*d,sy-0.1*d);
        painter->drawPath(path3);
        QPainterPath path4;
        path4.moveTo(sx-0.1*d,sy+0.3*d);
        path4.lineTo(sx-0.1*d,sy+0.1*d);
        path4.lineTo(sx-0.3*d,sy+0.1*d);
        painter->drawPath(path4);
    }

}


void DesktopItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

    QPen pen;
    pen.setStyle(Qt::SolidLine);
    pen.setColor(Qt::black);
    pen.setWidth(2);
    painter->setPen(pen);
    painter->drawRect(QRectF(sx,sy,8*d,9*d));

    //绘制横线
    for(int i=1;i<9;i++){
        painter->drawLine(QLineF(sx,sy+i*d,sx+8*d,sy+i*d));
    }

    //绘制竖线
    for(int i=1;i<8;i++){
        painter->drawLine(QLineF(sx+i*d,sy,sx+i*d,sy+4*d));
    }

    for(int i=1;i<8;i++){
        painter->drawLine(QLineF(sx+i*d,sy+5*d,sx+i*d,sy+9*d));
    }


    //绘制斜线
    painter->drawLine(QLineF(sx+3*d,sy,sx+5*d,sy+2*d));
    painter->drawLine(QLineF(sx+3*d,sy+2*d,sx+5*d,sy));
    painter->drawLine(QLineF(sx+3*d,sy+7*d,sx+5*d,sy+9*d));
    painter->drawLine(QLineF(sx+3*d,sy+9*d,sx+5*d,sy+7*d));


    //绘制楚河

    painter->setFont(QFont("隶书",r,QFont::Bold));
    QRect rect1(sx+1.2*d,sy+4.2*d,0.6*d,0.6*d);
    painter->drawText(rect1,"楚",QTextOption(Qt::AlignCenter));
    QRect rect2(sx+2.2*d,sy+4.2*d,0.6*d,0.6*d);
    painter->drawText(rect2,"河",QTextOption(Qt::AlignCenter));
    QRect rect3(sx+5.2*d,sy+4.2*d,0.6*d,0.6*d);
    painter->drawText(rect3,"汉",QTextOption(Qt::AlignCenter));
    QRect rect4(sx+6.2*d,sy+4.2*d,0.6*d,0.6*d);
    painter->drawText(rect4,"界",QTextOption(Qt::AlignCenter));

    //绘制标记
    drawLabelLine(sx,sy+3*d,0,painter,d);
    drawLabelLine(sx+2*d,sy+3*d,1,painter,d);
    drawLabelLine(sx+4*d,sy+3*d,1,painter,d);
    drawLabelLine(sx+6*d,sy+3*d,1,painter,d);
    drawLabelLine(sx+8*d,sy+3*d,2,painter,d);
    drawLabelLine(sx+7*d,sy+2*d,1,painter,d);
    drawLabelLine(sx+1*d,sy+2*d,1,painter,d);

    drawLabelLine(sx,sy+6*d,0,painter,d);
    drawLabelLine(sx+2*d,sy+6*d,1,painter,d);
    drawLabelLine(sx+4*d,sy+6*d,1,painter,d);
    drawLabelLine(sx+6*d,sy+6*d,1,painter,d);
    drawLabelLine(sx+8*d,sy+6*d,2,painter,d);
    drawLabelLine(sx+7*d,sy+7*d,1,painter,d);
    drawLabelLine(sx+1*d,sy+7*d,1,painter,d);

    //绘制可移动点位
    if(!poses.isEmpty()){
        painter->save();
        for(auto item:poses){
            painter->setBrush(Qt::yellow);
            double realCol=this->getSx()+item[0]*this->getD();
            double realRow=this->getSy()+item[1]*this->getD();
            int tr=0.4*this->getR();
            painter->drawEllipse(realCol-0.5*tr,realRow-0.5*tr,tr,tr);
        }
        painter->restore();
    }
    //绘制箭头 0 红色棋子在下方  1 黑色棋子在下方
    if(gameStart){
        painter->save();

        QPointF srPos;
        if(curSide==1){
            //红色棋子
            painter->setBrush(Qt::red);
            if(sideBelow==0){
                //红色棋子在下方
                srPos.setX(sx+9.5*d);
                srPos.setY(sy+7*d);
            }else{
                //红色棋子在上方
                srPos.setX(sx+9.5*d);
                srPos.setY(sy+2*d);
            }
        }else{
            //黑色棋子
            painter->setBrush(Qt::black);
            if(sideBelow==0){
                //红色棋子在下方
                srPos.setX(sx+9.5*d);
                srPos.setY(sy+2*d);
            }else{
                //红色棋子在上方
                srPos.setX(sx+9.5*d);
                srPos.setY(sy+7*d);
            }
        }
        QPolygonF headf;
        headf<<srPos<<QPointF(srPos.x()+0.3*d,srPos.y()-0.3*d)<<QPointF(srPos.x()+0.3*d,srPos.y()+0.3*d);
        QPainterPath path;
        path.addPolygon(headf);
        painter->drawPath(path);
        painter->restore();
    }

}

double DesktopItem::getD()
{
    return d;
}

double DesktopItem::getR()
{
    return r;
}

double DesktopItem::getSx()
{
    return sx;
}

double DesktopItem::getSy()
{
    return sy;
}

void DesktopItem::sideChange(int value,int sideBelow)
{
    qDebug()<<"desk sideValue here:"<<value<<" sideBelow:"<<sideBelow;
    this->curSide=value;
    this->sideBelow=sideBelow;

}

void DesktopItem::setGameStart(bool v)
{
    this->gameStart=true;
    qDebug()<<"game start Update";

}


void DesktopItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug()<<"desk event here";
    QGraphicsRectItem::mousePressEvent(event);
}

